<?php


namespace App\Job;


use App\Config\QueueConfig;
use App\Http\Library\Pfs;
use App\Http\Library\Queue;
use App\Http\Model\Job\PfsHeadAddclassincrementModel;
use App\Http\ModelDao\Job\PfsHeadAddclassincrementDao;

class PfsHeadAddclassincrementJob
{

    /**
     * 招行-售/退课汇总信息同步
     *
     * @param array $data
     *
     * @throws \Exception
     */
    public static function main(array $data)
    {
        $createDate = null;
        $statisticDate = null;
        $result = [];
        try {
            /*获取需要同步的classList结构*/
            $classList = PfsHeadAddclassincrementDao::classList($data, $createDate,$statisticDate);
            /*售/退课汇总信息同步   机构端 -> 招行*/
            $result = Pfs::pfsHeadAddclassincrement([
                /*机构编号，由招行分配*/
                'agencyId'      => $data['pfs_agencyid'],
                /*需要推送的T-1日班级总数*/
                'classNum'      => $data['add_class_increment_total'],
                /*本批次推送的班级数*/
                'batchClassNum' => count($classList),
                /*日期，格式：YYYY-MM-DD*/
                'date'          => $statisticDate,
                /*班级列表*/
                'classList'     => $classList,
            ], [
                /*招行-加密密钥*/
                'pfs_encrypt_private' => $data['pfs_encrypt_private'],
                /*招行-签名密钥*/
                'pfs_sign_private'    => $data['pfs_sign_private']
            ]);
            /*响应数据格式转换*/
            $result = PfsHeadAddclassincrementDao::result($result);
            /*同步结果入库*/
            PfsHeadAddclassincrementModel::save($data, $result);
        } catch (\Exception $exception) {
            log_arr([
                '[当日推送缴退费汇总-异常]',
                'line'   => $exception->getLine(),
                'file'   => $exception->getFile(),
                'msg'    => $exception->getMessage(),
                'data'   => serialize($data),
                'result' => $result,
            ], 'pfs');
            /*十分钟后尝试再次调用*/
            Queue::clock($data, 600);
            /*汇总数据同时完成后,执行当日班级&账户轧差净额计算*/
            Queue::clock([
                'pfs_dw_id'   => $data['pfs_dw_id'],
                'sleep'       => 1,
                'create_date' => $createDate
            ], 605, QueueConfig::CLASS_NETTING);
            /*同步结果入库*/
            PfsHeadAddclassincrementModel::save($data, $result, $exception->getMessage());
            throw new \Exception($exception->getMessage());
        }
    }


    /**
     * 班级交易明细-推送招行(班级汇总信息)
     *
     * @param $id   当日推送缴退费汇总表PayClassList（按班级汇总）同步记录表-主键ID
     *
     * @throws \Exception
     */
    public static function push(int $id)
    {
        log_arr([
            '[班级交易明细-推送]',
            'msg:推送指定汇总记录到招行',
            'id' => $id,
        ], 'pfs');
        /*数仓-当日推送缴退费汇总表PayClassList（按班级汇总）-获取需要写入任务队列的数据*/
        $addClassIncrement = PfsHeadAddclassincrementModel::addClassIncrement($id);
        if (empty($addClassIncrement)) {
            throw new \Exception('记录不存在');
        }
        /*查询监管账户信息*/
        $pfs = PfsHeadAddclassincrementModel::pfs($addClassIncrement->pfs_dw_id);
        /*要同步的数据写入队列*/
        Queue::rpush([
            'list'                      => [
                $addClassIncrement
            ],
            /*需要推送的T-1日班级总数*/
            'add_class_increment_total' => $addClassIncrement->pfs_class_num,
            /*招行&数仓-监管账户主体配置信息 表主键ID*/
            'pfs_dw_id'                 => $pfs->id,
            /*招行&数仓-监管账户主体配置信息 机构编码*/
            'pfs_agencyid'              => $pfs->pfs_agencyid,
            /*招行-加密密钥*/
            'pfs_encrypt_private'       => $pfs->pfs_encrypt_private,
            /*招行-签名密钥*/
            'pfs_sign_private'          => $pfs->pfs_sign_private,
        ], QueueConfig::PFS_HEAD_ADDCLASSINCREMENT);
        /*汇总数据同时完成后,执行当日班级&账户轧差净额计算*/
        Queue::clock([
            'pfs_dw_id'   => $pfs->id,
            'create_date' => $addClassIncrement->create_date,
            'class_id'    => $addClassIncrement->class_id,
        ], 5, QueueConfig::CLASS_NETTING);
    }


}





/**
 * PfsHeadAddclassincrementJob.php
 *
 * 说明:
 *
 * 修改历史
 * ----------------------------------------
 * 2021/6/7 15:09:00   操作:创建
 **/
